package com.service.impl;

import com.bean.Student;
import com.dao.StudentDao;
import com.dao.impl.StudentDaoImpl;
import com.service.StudentService;
import com.sun.org.apache.regexp.internal.RE;
import com.utils.Page;
import com.utils.WebUtils;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    StudentDao studentDao = new StudentDaoImpl();

    @Override
    public int login(Student record) {
        int status = existsUsername(record.getNo());
        if (status == 0) {
            Student student = studentDao.selectBy(record);
            if (student != null) {
                WebUtils.copyBean(student, record);
            } else {
                status = 2;
            }
        }
        return status;
    }

    @Override
    public int existsUsername(String username) {
        int status = 1;
        Student student = new Student();
        student.setNo(username);
        student = studentDao.selectBy(student);
        if (student != null) {
            if (student.getNo().equals(username)) {
                status = 0;
            }
        }
        return status;
    }

    @Override
    public int updatePassword(Integer id, String password, String newPassword) {
        int status = 0;
        Student student = new Student();
        student.setId(id);
        student.setPassword(password);
        student = studentDao.selectBy(student);
        if (student != null) {
            student.setPassword(newPassword);
            studentDao.updateByPrimaryKey(student);
        } else {
            status = -1;
        }
        return status;
    }

    @Override
    public int updeteInfo(Student record) {
        int status = 0;
        if (studentDao.updateByPrimaryKey(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public int insert(Student record) {
        int status = 0;
        Student student = new Student();
        student.setNo(record.getNo());
        student = studentDao.selectBy(student);
        if (student != null && !student.getId().equals(record.getId())) {
            status = 1;
        } else if (studentDao.insert(record) == -1) {
            status = 1;
        }
        return status;
    }

    @Override
    public Page<Student> page(int pageNo, int pageSize, String content) {
        Page<Student> page = new Page<>();
        page.setPageSize(pageSize);
        Integer pageTotalCount = studentDao.selectForPageTotalCount(content);
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal = pageTotalCount / pageSize;
        if (pageTotalCount % pageSize > 0) {
            pageTotal += 1;
        }
        page.setPageTotal(pageTotal);
        page.setPageNo(pageNo);
        int begin = (page.getPageNo() - 1) * pageSize;
        List<Student> items = studentDao.selectForPageItems(begin, pageSize, content);
        // 设置当前页数据
        page.setItems(items);
        return page;
    }

    @Override
    public Student selectBy(Student record) {
        return studentDao.selectBy(record);
    }

    @Override
    public int deleteById(Integer id) {
        int status = 0;
        if (studentDao.deleteByPrimaryKey(id) == -1) {
            status = 1;
        }
        return status;
    }
}
